package com.cx.caigou.request.controller;

import com.cx.caigou.config.ResultVO;
import com.cx.caigou.config.ResultVOBuilder;
import com.cx.caigou.request.model.*;
import com.cx.caigou.request.service.RequestService;
import com.cx.caigou.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

@CrossOrigin
@RestController
@Api(value = "RequestController",description = "询价清单")
public class RequestController {

    @Autowired
    private RequestService requestService;

    @PostMapping("inquiryList/search")
    @ApiOperation(value = "分页查询")
    public ResultVO<AllrecordsVo<RequestClass>> search(@RequestBody RequestSelect requestSelect){
           return  requestService.queryData(requestSelect);
    }

    @GetMapping("legalPerson/ofCurrentUser")
    @ApiOperation(value = "法人下拉框")
    public ResultVO<AllrecordsVo<LegalPersonVO>> queryLegalPerson(){
        Integer id = 1;
        if (id==null){
            return ResultVOBuilder.error("用户ID不能为空！");
        }
        return requestService.queryLegalPerson(id);
    }

    @PostMapping("metadata/getPullDownList")
    @ApiOperation(value = "询价类型/状态/含税/币别下拉框")
    public ResultVO queryStatus(){
        HashMap<String , ArrayList<StatusVo>> map = new HashMap<>();
        //询价类型
        ArrayList<StatusVo> listType = new ArrayList<>();
        for (TypeEnum s:TypeEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setLabel(s.getStatusName());
            statusVo.setValue(s.getStatus());
            listType.add(statusVo);
        }
        map.put("type",listType);
        //询价状态
        ArrayList<StatusVo> listStatus = new ArrayList<>();
        for (StatusEnum s:StatusEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setLabel(s.getStatusName());
            statusVo.setValue(s.getStatus());
            listStatus.add(statusVo);
        }
        map.put("inquiryStatus",listStatus);

        //含税
        ArrayList<StatusVo> listTax = new ArrayList<>();
        for (TaxEnum s:TaxEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setLabel(s.getStatusName());
            statusVo.setValue(s.getStatus());
            listTax.add(statusVo);
        }
        map.put("isTax",listTax);

        //币别
        ArrayList<StatusVo> listCurrencyCode = new ArrayList<>();
        for (CurrencyCodeEnum s:CurrencyCodeEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setLabel(s.getStatusName());
            statusVo.setValue(s.getStatus());
            listCurrencyCode.add(statusVo);
        }
        map.put("currencyCode",listCurrencyCode);

        return ResultVOBuilder.success(map);

    }

    @GetMapping("partner/queryByLegalPerson")
    @ApiOperation(value = "供应商下拉框")
    public ResultVO queryByLegalPerson(Integer legalPersonId){

        if(legalPersonId == null){
            ResultVOBuilder.error("法人id不能为空");
        }
        return requestService.queryByLegalPerson(legalPersonId);
    }

    @PostMapping("inquiryList/selectMaterial")
    @ApiOperation(value = "询价明细新增")
    public ResultVO selectMaterial(@RequestBody SelectSpare selectSpare){
        return requestService.selectMaterial(selectSpare);
    }

    @PostMapping("material/searchPartnerPage")
    @ApiOperation(value = "询价供应商")
    public ResultVO searchPartnerPage(@RequestBody SelectSupplier selectSupplier){
        System.out.println(selectSupplier);
        return requestService.searchPartnerPage(selectSupplier);
    }


    @PostMapping("inquirySheet/see")
    @ApiOperation(value = "查看")
    public ResultVO<AllrecordsVo<AllClass>> see(@RequestBody ID id){
        System.out.println(id);

        return requestService.see(id.getRequestForQuotationId());
    }

    @PostMapping("inquiryList/add")
    @ApiOperation(value = "询价新增")
    public ResultVO add(@RequestBody @Valid  AllClass allClass ){
        System.out.println("新增"+allClass);
        return requestService.add(allClass);
    }


    @PostMapping("inquiryList/update")
    @ApiOperation(value = "询价修改")
    public ResultVO update(@RequestBody @Valid AllClass allClass){
        System.out.println("修改"+allClass);
        return requestService.update(allClass);
    }

    @PostMapping("quotation/InquiryIntroduction")
    @ApiOperation(value = "备件明细导入")
    public ResultVO importFile(MultipartFile file)throws Exception{
        System.out.println(file.getOriginalFilename());
        if (!file.getOriginalFilename().contains("xlsx")){
            return ResultVOBuilder.error("请检查导入模板，尾缀名必须为xlsx");
        }

        ResultVO resultVO = requestService.importFile(file);

        return ResultVOBuilder.success(resultVO);

    }
    @PostMapping("inquiryList/modifyState")
    @ApiOperation(value = "作废")
    public ResultVO modifyState(@RequestBody ID requestForQuotationId ){
        return requestService.modifyState(requestForQuotationId);
    }

    @PostMapping("inquiryAuditor/update")
    @ApiOperation(value = "审核")
    public ResultVO auditorState(@RequestBody ID requestForQuotationId){
        return requestService.updateState(requestForQuotationId);
    }

    @PostMapping("inquiryApprover/update")
    @ApiOperation(value = "审批")
    public ResultVO approverState(@RequestBody ID requestForQuotationId){
        return requestService.approverState(requestForQuotationId);
    }

//    @PostMapping("quotationList/searchPre")
//    @ApiOperation(value = "报价列表")
//    public ResultVO searchPre( ){
//        return requestService.query();
//    }



}
